var body = document.getElementsByTagName('body')[0];
var c = document.getElementById('game');
var ctx = c.getContext('2d');
var gradient=ctx.createLinearGradient(0,0,c.width,0);
gradient.addColorStop("0","magenta");
gradient.addColorStop("0.5","blue");
gradient.addColorStop("1.0","red");
ctx.fillStyle = gradient;
ctx.font = "30px TheSansBlack";
var map = 10;
var life = 3;
var side = 1;
var score = 0;
var rane;
var ranq;

// Question & Answer
var quest = new Array();
	quest [0] = "7+9 = ?";	
	quest[1] = "8-5 = ?";
	quest[2] = "5*4 = ?";
	quest[3] = "9/3 = ?";
	quest[4] = "6+2 = ?";
 	quest[5] = "6-4 = ?";
 	quest[6] = "9*2 = ?";
 	quest[7] = "8/2 = ?";
 	quest[8] = "1+4 = ?";
 	quest[9] = "6-0 = ?";
 	quest[10] = "4*2 = ?";
 	quest[11] = "8/4 = ?";
 	quest[12] = "8+0 = ?";
 	quest[13] = "5-3 = ?";
 	quest[14] = "2*3 = ?";
 	quest[15] = "2/1 = ?";
 	quest[16] = "3+9 = ?";
 	quest[17] = "8-6 = ?";
 	quest[18] = "7*6 = ?";
 	quest[19] = "4/4 = ?";
 	quest[20] = "0+8 = ?";
 	quest[21] = "2-1= ?";
 	quest[22] = "7*1 = ?";
 	quest[23] = "6/3 = ?";
 	quest[24] = "6+5= ?";
 	quest[25] = "3-1 = ?";
 	quest[26] = "8*4 = ?";
 	quest[27] = "3/3 = ?";
	quest[28] = "6+7 = ?";
	quest[29] = "9-9 = ?";
	quest[30] = "8*8 = ?";
	quest[31] = "0/9 = ?";
	quest[32] = "3+2 = ?";
	quest[33] = "8-7 = ?";
 	quest[34] = "11*2 = ?";
 	quest[35] = "99/11 = ?";
 	quest[36] = "34+45 = ?";
 	quest[37] = "43-21 = ?";
 	quest[38] = "2*12 = ?";
 	quest[39] = "100/5 = ?";
	quest[40] = "34+67 = ?";
	quest[41] = "87-31 = ?";
	quest[42] = "12*1 = ?";
	quest[43] = "30/5 = ?";
	quest[44] = "43+10 = ?";
	quest[45] = "21-12 = ?";
	quest[46] = "3*40 = ?";
	quest[47] = "98/2 = ?";
	quest[48] = "62+10 = ?";
	quest[49] = "31-11 = ?";
	quest[50] = "1*20 = ?";
	quest[51] = "50/5 = ?";
	quest[52] = "20+90 = ?";
	quest[53] = "60-15 = ?";
	quest[54] = "8*20 = ?";
	quest[55] = "80/20 = ?";
	quest[56] = "69+19 = ?";
	quest[57] = "68-45 = ?";
	quest[58] = "45*2 = ?";
	quest[59] = "90/30 = ?";
	quest[60] = "77+22 = ?";
	quest[61] = "78-23 = ?";
	quest[62] = "6*30 = ?";
	quest[63] = "75/5 = ?";
	quest[64] = "23+19 = ?";
	quest[65] = "23-19 = ?";
	quest[66] = "12*3 = ?";
	quest[67] = "132+156 = ?";
	quest[68] = "700-345 = ?";
	quest[69] = "123*2 = ?";
	quest[70] = "300/100 = ?";
	quest[71] = "931+100 = ?";
	quest[72] = "600-350 = ?";
	quest[73] = "345*2 = ?";
	quest[74] = "500/5 = ?";
	quest[75] = "902+208 = ?";
	quest[76] = "743-243 = ?";
	quest[77] = "950/50 = ?";
	quest[78] = "111+999 = ?";
	quest[79] = "999-111 = ?";
	quest[80] = "150*6 = ?";
	quest[81] = "150/50 = ?";
	quest[82] = "401+201 = ?";
	quest[83] = "605-500 = ?";
	quest[84] = "400*5 = ?";
	quest[85] = "400/2 = ?";
	quest[86] = "400+500 = ?";
	quest[87] = "590-490 = ?";
	quest[88] = "160*2 = ?";
	quest[89] = "470/10 = ?";
	quest[90] = "123+456 = ?";
	quest[91] = "456-123 = ?";
	quest[92] = "650*1 = ?";
	quest[93] = "980/10 = ?";
	quest[94] = "432+123 = ?";
	quest[95] = "432-123 = ?";
	quest[96] = "600*6 = ?";
	quest[97] = "660/6 = ?";
	quest[98] = "784+100 = ?";
	quest[99] = "456-400 = ?";

var ans = new Array();
	ans[0] = "16";
	ans[1] = "3";
	ans[2] = "20";
	ans[3] = "3";
	ans[4] = "8";
	ans[5] = "2";
	ans[6] = "18";
	ans[7] = "4";
	ans[8] = "5";
	ans[9] = "6";
	ans[10] = "8";
	ans[11] = "2";
	ans[12] = "8";
	ans[13] = "2";
	ans[14] = "6";
	ans[15] = "2";
	ans[16] = "12";
	ans[17] = "2";
	ans[18] = "42";
	ans[19] = "1";
	ans[20] = "8";
	ans[21] = "1";
	ans[22] = "7";
	ans[23] = "2";
	ans[24] = "11";
	ans[25] = "2";
	ans[26] = "32";
	ans[27] = "1";
	ans[28] = "13";
	ans[29] = "0";
	ans[30] = "64";
	ans[31] = "0";
	ans[32] = "5";
	ans[33] = "1";
	ans[34] = "22";
	ans[35] = "9";
	ans[36] = "79";
	ans[37] = "22";
	ans[38] = "24";
	ans[39] = "20";
	ans[40] = "101";
	ans[41] = "56";
	ans[42] = "12";
	ans[43] = "6";
	ans[44] = "53";
	ans[45] = "9";
	ans[46] = "120";
	ans[47] = "49";
	ans[48] = "72";
	ans[49] = "20";
	ans[50] = "20";
	ans[51] = "10";
	ans[52] = "110";
	ans[53] = "45";
	ans[54] = "160";
	ans[55] = "4";
	ans[56] = "88";
	ans[57] = "23";
	ans[58] = "90";
	ans[59] = "3";
	ans[60] = "99";
	ans[61] = "55";
	ans[62] = "180";
	ans[63] = "15";
	ans[64] = "42";		
	ans[65] = "4";
	ans[66] = "36";
	ans[67] = "288";
	ans[68] = "355";
	ans[69] = "246";
	ans[70] = "3";
	ans[71] = "1031";
	ans[72] = "250";
	ans[73] = "690";
	ans[74] = "100";
	ans[75] = "1110";
	ans[76] = "500";
	ans[77] = "19";
	ans[78] = "1110";
	ans[79] = "888";
	ans[80] = "900";
	ans[81] = "3";
	ans[82] = "602";
	ans[83] = "105";
	ans[84] = "2000";
	ans[85] = "200";
	ans[86] = "900";
	ans[87] = "100";
	ans[88] = "320";
	ans[89] = "47";
	ans[90] = "579";
	ans[91] = "333";
	ans[92] = "650";
	ans[93] = "98";
	ans[94] = "555";
	ans[95] = "309";
	ans[96] = "3600";
	ans[97] = "110";
	ans[98] = "884";
	ans[99] = "56";

// Protagonist
var hside = new Array();
	hside[0] = "character/hero_u.png";
	hside[1] = "character/hero_d.png";
	hside[2] = "character/hero_l.png";
	hside[3] = "character/hero_r.png";
var him = new Image();

// Antagonist
var enemy = new Image();
var smalle = "character/smallpriest.png";
var bige = "character/bigpriest.png";
var epic = 0;

// Function
function ranMap(a)  //random map
{
	if(a==0)
	{
		do
		{
			rmap = Math.round(Math.random()*10);
		}
		while(rmap==2 || rmap==5 || rmap==8 || rmap==10);
	}
	if(a==1)
	{
		do
		{
			rmap = Math.round(Math.random()*10);
		}
		while(rmap==4 || rmap==5 || rmap==7 || rmap==9 || rmap==10);
	}
	if(a==2)
	{
		do
		{
			rmap = Math.round(Math.random()*10);
		}
		while(rmap==3 || rmap==6 || rmap==7 || rmap==10);
	}
	if(a==3)
	{
		do
		{
			rmap = Math.round(Math.random()*10);
		}
		while(rmap==1 || rmap==6 || rmap==8 || rmap==9 || rmap==10);
	}
	return rmap;
}
function changBG()  //change background
{
	if(map==0)
	{
		body.style.backgroundImage = 'url(BG/game0.png)';
	}
	else if(map==1)
	{
		body.style.backgroundImage = 'url(BG/game1.png)';
	}
	else if(map==2)
	{
		body.style.backgroundImage = 'url(BG/game2.png)';
	}
	else if(map==3)
	{
		body.style.backgroundImage = 'url(BG/game3.png)';
	}
	else if(map==4)
	{
		body.style.backgroundImage = 'url(BG/game4.png)';
	}
	else if(map==5)
	{
		body.style.backgroundImage = 'url(BG/game5.png)';
	}
	else if(map==6)
	{
		body.style.backgroundImage = 'url(BG/game6.png)';
	}
	else if(map==7)
	{
		body.style.backgroundImage = 'url(BG/game7.png)';
	}
	else if(map==8)
	{
		body.style.backgroundImage = 'url(BG/game8.png)';
	}
	else if(map==9)
	{
		body.style.backgroundImage = 'url(BG/game9.png)';
	}
	rane = Math.round(Math.random()*2);
}
function drawHero(nh,x,y)
{
	him.onload = function()
	{
		ctx.drawImage(him,x,y);
	};
	him.src = hside[nh];
}
function mapBound(x,y)
{
	if(map==0)
	{
		if((x>255 && x<440) || (y>165 && y<305))
		{
			return true;
		}
	}
	if(map==1)
	{
		if((x>255 && x<440) || (y>165 && y<305 && x>255))
		{
			return true;
		}
	}
	if(map==2)
	{
		if((x>255 && x<440 && y<305) || (y>165 && y<305))
		{
			return true;
		}
	}
	if(map==3)
	{
		if((x>255 && x<440) || (y>165 && y<305 && x<440))
		{
			return true;
		}
	}
	if(map==4)
	{
		if((x>255 && x<440 && y>165) || (y>165 && y<305))
		{
			return true;
		}
	}
	if(map==5)
	{
		if(y>180 && y<290)
		{
			return true;
		}
	}
	if(map==6)
	{
		if(x>270 && x<425)
		{
			return true;
		}
	}
	if(map==7)
	{
		if((x>255 && x<440 && y>165) || (y>165 && y<305 && x<440))
		{
			return true;
		}
	}
	if(map==8)
	{
		if((x>255 && x<440 && y<305) || (y>165 && y<305 && x>255))
		{
			return true;
		}
	}
	if(map==9)
	{
		if((x>255 && x<440 && y>165) || (y>165 && y<305 && x>255))
		{
			return true;
		}
	}
	if(map==10)
	{
		if((x>255 && x<440 && y<350))
		{
			return true;
		}
	}
	return false;
}
function move()
{
	if(!epic)
	{
		if(37 in keysDown)
		{
			if(prot.x>5)
			{
				ctx.clearRect(prot.x,prot.y,100,100);
				if(mapBound(prot.x,prot.y))prot.x -= 10;
				else prot.x += 20;
				side = 2;
			}
		}
		if(38 in keysDown)
		{
			if(prot.y>5)
			{
				ctx.clearRect(prot.x,prot.y,100,100);
				if(mapBound(prot.x,prot.y))prot.y -= 10;
				else prot.y += 20;
				side = 0;
			}
		}
		if(39 in keysDown)
		{
			if(prot.x<695)
			{
				ctx.clearRect(prot.x,prot.y,100,100);
				if(mapBound(prot.x,prot.y))prot.x += 10;
				else prot.x -= 20;
				side = 3;
			}
		}
		if(40 in keysDown)
		{
			if(prot.y<495)
			{
				ctx.clearRect(prot.x,prot.y,100,100);
				if(mapBound(prot.x,prot.y))prot.y += 10;
				else prot.y -= 20;
				side = 1;
			}
		}
	}
	drawHero(side,prot.x,prot.y);
}
function changeMap()
{
	if(prot.x<20 || prot.x>670 || prot.y<20 || prot.y>470)
	{
		if(prot.x<20) // left
		{
			map = ranMap(2);
			ctx.clearRect(prot.x,prot.y,100,100);
			prot.x = 670;
			prot.y = 275;
		}
		if(prot.x>670) // right
		{
			map = ranMap(3);
			ctx.clearRect(prot.x,prot.y,100,100);
			prot.x = 30;
			prot.y = 275;
		}
		if(prot.y<20) // up
		{
			map = ranMap(0);
			ctx.clearRect(prot.x,prot.y,100,100);
			prot.x = 350;
			prot.y = 470;
		}
		if(prot.y>470) // down
		{
			map = ranMap(1);
			ctx.clearRect(prot.x,prot.y,100,100);
			prot.x = 350;
			prot.y = 30;
		}
		score += 1;
		changBG();
		drawHero(side,prot.x,prot.y);
	}
}
function callScore()
{
	ctx.clearRect(500,10,300,100);
	ctx.fillText("Score : "+score,615,43);
}
function drawEnemy(a)
{
	if(a==0)
	{
		enemy.onload = function()
		{
			ctx.drawImage(enemy,325,225);
		};
		enemy.src = smalle;
	}
	else
	{
		enemy.onload = function()
		{
			ctx.drawImage(enemy,100,100);
		};
		enemy.src = bige;
	}
}
function meetEnemy()
{
	if(rane==2)
	{
		drawEnemy(epic);
		if(prot.x+40>=325 && prot.x<=440 && prot.y+40>=225 && prot.y<=340)
		{
			ctx.clearRect(325,225,150,150);
			epic = 1;
			popQuest();
		}
	}
}
function popQuest()
{
	ranq = Math.round(Math.random()*99);
	ctx.clearRect(50,50,250,250);
	ctx.fillText(quest[ranq],300,250);
}

// GAME
var prot = {x:350,y:250};
drawHero(side,prot.x,prot.y);
var keysDown = {}; 
	addEventListener("keydown", function (e) { keysDown[e.keyCode] = true; }, false); 
	addEventListener("keyup", function (e) { delete keysDown[e.keyCode]; }, false);
var start = function()
{
	move();
	changeMap();
	callScore();
	meetEnemy();
};
setInterval(start,50);